<?php
   session_start(); 
      if ($_REQUEST['host']) {
      $_SESSION['host'] =  $_REQUEST['host'];
      }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
        <link rel="stylesheet" type="text/css" href="iphone.css" title="style" />
        <link rel="stylesheet" type="text/css" href="custom.css" title="style" />
        <script type="text/javascript" src="lib/prototype.js"></script>
        <script type="text/javascript" src="lib/scriptaculous.js"></script>
        <script type="text/javascript" src="server.js"></script>
        <script type="text/javascript" src="custom.js"></script>
        <?php
        $edit = isset($_REQUEST['edit']);
        $demo = isset($_REQUEST['demo']);
        if ($edit) {
          echo '<link rel="stylesheet" type="text/css" href="custom_edit.css" title="style" />' . "\n";
        }
           if ($demo) {
           echo '<script type="text/javascript" src="custom_demo.js"></script>';
           }


// Possibly read the buttons
// Make sure they're in order
$ids2strings = array();
foreach ($_REQUEST as $k => $v) {

  if (preg_match('/_button_(\d+)/',$k,$out)) {
    $ids2strings[$out[1]] = $v;
  }
}
ksort($ids2strings);
echo '<script type="text/javascript">' . "\n";
echo 'Event.observe(window,"load", function() {' . "\n";
echo ' var b;' . "\n";
foreach ($ids2strings as $id => $str) {
  $parts = preg_split('/\|/',$str);
  $ks2vs = array();
  foreach ($parts as $part) {
    $kv = preg_split('/=/',$part);
    $k = $kv[0];
    $v = $kv[1];
    $ks2vs[$k] = $v;
  }
  echo ' b = newButtonInstance(';
  echo '"' . $ks2vs['image'] . '"';
  echo ',';
  echo '"' . $ks2vs['title'] . '"';
  echo ',';
  echo 'false';
  echo ');';
  echo "\n";
  echo ' b.show();';
  echo "\n";
  $x = $ks2vs['x'];
  if ($x) {
    echo 'b.setX(' . $x . ');';
    echo "\n";
  }
  $y = $ks2vs['y'];
  if ($y) {
    echo 'b.setY(' . $y . ');';
    echo "\n";
  }
  if ($ks2vs['newline']) {
    echo 'b.setNewline(true);';
    echo "\n";
    echo '$n("br",$("buttons"))';
    echo "\n";
  }
}
echo '});' . "\n";
echo '</script>' . "\n";
           
        ?>
        
  </head>
  <body
<?php
  if ($demo) {
    echo 'onload="demo();"';
  }
?>
  >
  <?php if ($edit) { ?>
  <div id="siteWrapper">
    <div id="editpane">
      <div class="title">edit</div>
      <div class="text">
      Please select a button by clicking on it.  You can then edit it
      by dragging it or filling in the information in the area below.
      </div>
        <div id="buttonarea" class="text">
          <form action="return false;" onsubmit="save(); return false">
        <table class="edittable">
        <?php
        function newRow($id,$name,$value='',$isImage=FALSE) {
               echo ' <tr class="editrow">' . "\n";
               echo '  <td id="edit_' . $id . '_name" class="editname">' . $name . ':</td>' . "\n";
               echo '  <td><input id="edit_' . $id . '_value" class="editvalue" value="..."></input>';
               if ($isImage) {
                 echo '<br/>';
                 echo '<img id="image_preview" src=""></img>';
               }
               echo '</td>' . "\n";
               echo ' </tr>' . "\n";
             }
             newRow('title','Title','');
             newRow('command','Command','');
             newRow('x','X coord','');
             newRow('y','Y coord','');
             newRow('image','Image','',TRUE);
             ?>
        <tr>
          <td></td>
          <td>
            <button onclick="save(); return false">Save</button>
          </td>
        </tr>
        </table>
</form>
          
          <div id="otherButtons">
                <button onclick="newButton(); return false">New</button>
                <button onclick="createLink(); return false">Link</button>
                <button onclick="reset(); return false">Reset</button>
          </div>
          
        </div>
    </div>
  <?php } ?>

    <div id="site">
      <?php
         function button($icon,$name=null) {
         if ($name == null) $name = $icon;
         echo '<span class="b">';
         if ($edit) {
           $onClick = 'google'.  $name . '()';
         } else {
           $onClick = 'edit(this)';
         }
         echo '<a border="0" href="#" onclick="' . $onClick . '; return false">';
         echo '<img border="0" src="icons/' . $icon . '.png"></a>';
         echo '</span>';
         }

        echo '<p id="buttons">';
      if (FALSE && !$edit) {
        button('Safari','Browser');
        button('Calendar');
        button('Stocks');
        echo '<br/>';
        
        button('Maps');
        button('Photos');
        button('Notes','Reader');
        echo '<br/>';
        button('YouTube');
        button('Mail');
        button('Weather');
      }
echo '</p>';
?>

      
      <?php if (!$edit) { ?>
  <div style="position:absolute; left:10px; top:230px" id="host">
            
            <?php include 'host.php'; ?>
            <?php
                $link = $_SERVER['PHP_SELF'];
  $link .= '?edit=1';
  foreach ($_REQUEST as $k => $v) {
    $link .= '&' . $k . '=' . $v;
  }
  echo '<br/>';
  echo '[<a href="' . $link . '">edit</a>]';
?>
</div>
            <?php } ?>

      <div id="mainJS">
        <!-- the communication is done here -->
      </div>

    </div>

  <?php if ($edit) echo '</div>'; ?>

  </body>
</html>

